
<div ng-controller="prodQueryCtl">

	<div class="wrapper wrapper-content animated fadeInRight">
		<div class="row">
			<div class="col-lg-12">
				<div class="ibox">
					<div class="ibox-content">
						<form role="form" class="form-horizontal">
							<div class="form-group">
								<label class="col-sm-2 control-label">商品类目</label>
								<div class="col-sm-10">
									<select id="class" chosen disable-search="true"
										class="chosen-select" no-results-text="'没有找到相应条目'"
										ng-model="prodcatSel" data-placeholder-text-single="'请选择...'"
										ng-options="item.routename for item in prodcatOpt">
										<option value=""></option>
									</select>
								</div>
							</div>
						</form>
						<button ng-click="query()" class="btn btn-sm btn-primary"
							type="submit">查询</button>

						<button ng-click="prod_off()" class="btn btn-sm btn-primary"
							type="submit">下架</button>

						<button ng-click="prod_on()" class="btn btn-sm btn-primary"
							type="submit">上架</button>

						<button ng-click="prodsaleattrModify()"
							class="btn btn-sm btn-primary" type="submit">修改规格</button>

						<button ng-click="prodModify()" class="btn btn-sm btn-primary"
							type="submit">更新资料</button>
						<button ng-click="prodPicsUpdate()" class="btn btn-sm btn-primary"
							type="submit">更新产品图</button>

						<button ng-click="del()" class="btn btn-sm btn-primary"
							type="submit">删除</button>
					</div>
				</div>
			</div>
		</div>
		<div class="row">
			<div class="col-lg-12">
				<div class="ibox">
					<div class="ibox-content">
						<table datatable="ed" dt-options="dtOptions"
							dt-instance="dtInstance" dt-columns="dtColumns"
							dt-column-defs="dtColumnDefs" class="table table-hover">
						</table>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">
//原始属性集
var keys = [ [10, 11, 12], [20, 21], [30, 31, 32] ];
//测试结果集
var data = {
    "10,20,30": {
        price: 100,
        count: 1
    }
}
//保存最后的组合结果信息
var SKUResult = {};
//获得对象的key
function getObjKeys(obj) {
    if (obj !== Object(obj)) throw new TypeError('Invalid object');
    var keys = [];
    for (var key in obj)
        if (Object.prototype.hasOwnProperty.call(obj, key))
            keys[keys.length] = key;
    return keys;
}
//把组合的key放入结果集SKUResult
function add2SKUResult(key, sku) {
    if(SKUResult[key]) {//SKU信息key属性·
        SKUResult[key].count += sku.count;
        SKUResult[key].prices.push(sku.price);
    } else {
        SKUResult[key] = {
            count : sku.count,
            prices : [sku.price]
        };
    }
}
//对一条SKU信息进行拆分组合
function combineSKU(skuKeyAttrs, cnum, sku) {
    var len = skuKeyAttrs.length;
    for(var i = 0; i < len; i++) {
        var key = skuKeyAttrs[i];
        for(var j = i+1; j < len; j++) {
            if(j + cnum <= len) {
                var tempArr = skuKeyAttrs.slice(j, j+cnum);	//安装组合个数获得属性值·
                var genKey = key + "," + tempArr.join(",");	//得到一个组合key
                add2SKUResult(genKey, sku);
            }
        }
    }
}
//初始化得到结果集
function initSKU() {
    var i, j, skuKeys = getObjKeys(data);
    for(i = 0; i < skuKeys.length; i++) {
        var skuKey = skuKeys[i];//一条SKU信息key
        var sku = data[skuKey];	//一条SKU信息value
        var skuKeyAttrs = skuKey.split(","); //SKU信息key属性值数组
        var len = skuKeyAttrs.length;

        //对每个SKU信息key属性值进行拆分组合
        for(j = 0; j < len; j++) {
			//单个属性值作为key直接放入SKUResult
            add2SKUResult(skuKeyAttrs[j], sku); 
			//对本组SKU信息key属性进行组合，组合个数为j
            (j > 0 && j < len-1) && combineSKU(skuKeyAttrs, j, sku); 
        }

        //结果集接放入SKUResult
        SKUResult[skuKey] = {
            count:sku.count,
            prices:[sku.price]
        }

    }
    console.log(SKUResult);
}
initSKU();
</script>




